package com.sptpc.learnspring;

import com.sptpc.learnspring.common.OrderState;
import com.sptpc.learnspring.model.Category;
import com.sptpc.learnspring.model.Drink;
import com.sptpc.learnspring.model.Order;
import com.sptpc.learnspring.service.CategoryService;
import com.sptpc.learnspring.service.DrinkService;
import com.sptpc.learnspring.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;


@Slf4j
@SpringBootApplication
public class LearnSpringApplication implements ApplicationRunner {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DrinkService drinkService;
    @Autowired
    private OrderService orderService;

    public static void main(String[] args) {
        SpringApplication.run(LearnSpringApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        drinkCRUD();
    }
    //用户订单
    private void customerOrderCRUD() {
//        用户的所有订单
//        List<Order> orders = orderService.getOrders("张三");
//        新增参数，找寻客户到第index个订单
        Order order = orderService.getOrder("张三",0);
        orderService.changeState(order, OrderState.BREWING);
    }
    //订单信息
    private void orderCRUD() {

        Drink drink1 = drinkService.getDrink("葡萄汁");
        Drink drink2 = drinkService.getDrink("奶茶");
        Order order = orderService.saveOrder("张三",drink1,drink2);
        log.info("订单创建成功:{}",order);
    }
    //饮品
    private void drinkCRUD() {
        // 模拟在分类下新建饮品
        Category search = categoryService.findOneT("果汁");
        Drink drink = drinkService.saveDrink(
                Drink.builder().name("苹果汁")
                        .price(Money.of(CurrencyUnit.of("CNY"),8.00))
                        .category(search) // 这行代码是维护一对多关系的关键
                        .build()
        );
        log.info("创建一个新的饮品:{}", drink);
//        List<Drink> drinks = drinkService.getAllDrinks(search.getId());
//        drinks.forEach(drink -> log.info("包含饮品:{}：",drink));
    }
    //种类
    private void categoryCRUD() {

//        插入语句
        Category insert = categoryService.saveCategory("咖啡");
        log.info("创建一个新的分类:{}",insert);

//        查询语句：查询所有分类
        List<Category> categoryList = categoryService.findALl();
        categoryList.forEach(c ->{
            log.info("分类:{}",c);
        });

//        查询语句：查找单一分类
//       Category select =   categoryService.findOne("果%");
         Category select =   categoryService.findOneT("果%");
                    log.info("查找分类:{}",select);


//        更新语句：更新分类
          Category update = categoryService.update(1L,"鲜榨果汁");
                            log.info("更新分类:{}",update);


//      删除语句：通过返回值判断是否删除成功
        if (categoryService.delete(3L)) {
            log.info("删除成功！");
        }else {
            log.error("删除失败");
        }
    }
}
